#!/usr/bin/env python

import argparse, ConfigParser, logging
from SOAPpy import SOAPProxy

logging.basicConfig(filename='snow_ticketer.log', format='%(asctime)s %(levelname)s: %(message)s', level=logging.DEBUG)

conf = 'config.ini'
fc = open(conf)
config = ConfigParser.RawConfigParser()
config.readfp(fc)
username = config.get('snow', 'username')
password = config.get('snow', 'password')
instance = config.get('snow', 'instance')
fc.close()

proxy = 'https://%s:%s@%s.service-now.com/u_spectrum_incident.do?SOAP' % (username, password, instance)
namespace = 'http://%s.service-now.com/' % (instance)

def createincident(params_dict):
	server = SOAPProxy(proxy, namespace)
	
	try:
		response = server.insert(cmdb_ci=(params_dict['host']), impact=(params_dict['impact']), assignment_group=(params_dict['group']), short_description=(params_dict['shortdesc']), description=(params_dict['desc']))
		return (response)
	except:
		print ('Error, error')
		logging.error('Error, error')
		exit (1)

def main():
	argp = argparse.ArgumentParser(description="Create incident in ServiceNow", prog="SetScript.py")
	argp.add_argument("--host", type=str, help="Host name in CMDB")
	argp.add_argument("--state", type=str, help="Host state")
	argp.add_argument("--impact", type=int, help="SNow impact")
	argp.add_argument("--group", type=str, help="SNow resolver group")
	argp.add_argument("--shortdesc", type=str, help="SNow short description")
	argp.add_argument("--desc", type=str, help="SNow description")

	options = argp.parse_args()
	options = vars(options)
	
	print(options)
	new_incident_sysid=createincident(options)
	print(new_incident_sysid)

	incident, status, action = 'INC007', 'created', 'create' 
	logging.info('Incident:%s Status:%s Action:%s', incident, status, action)

if __name__ == '__main__':
	main()
